local WEED_DEFS = require("prefabs/weed_defs").WEED_DEFS
local assets =
{
    Asset("ANIM", "anim/pastoral_daogui_ganoderma.zip"),
}

-- 可以农场土堆栽种
local function can_plant_seed(inst, pt, mouseover, deployer)
    local x, z = pt.x, pt.z
    return TheWorld.Map:CanTillSoilAtPoint(x, 0, z, true)
end

-- 植物人专属部署函数
local function OnDeploy(inst, pt, deployer)
    local plant = SpawnPrefab(inst.components.farmplantable.plant)
    plant.Transform:SetPosition(pt.x, 0, pt.z)
    plant:PushEvent("on_planted", { in_soil = false, doer = deployer, seed = inst })
    TheWorld.Map:CollapseSoilAtPoint(pt.x, 0, pt.z)
    inst:Remove()
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("pastoral_daogui_ganoderma")
    inst.AnimState:SetBuild("pastoral_daogui_ganoderma")
    inst.AnimState:PlayAnimation("idle")

    inst:AddTag("cattoy") -- 猫猫玩具

    MakeInventoryFloatable(inst, "med", 0.05, 0.68)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inventoryitem")

    inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

    inst:AddComponent("edible")
    inst.components.edible.foodtype = FOODTYPE.VEGGIE
    inst.components.edible.healthvalue = 5
    inst.components.edible.hungervalue = 15
    inst.components.edible.sanityvalue = 3

    inst:AddComponent("inspectable")
    inst:AddComponent("tradable")

    inst:AddComponent("perishable")
    inst.components.perishable:SetPerishTime(2400) -- 40分钟
    inst.components.perishable:StartPerishing()
    inst.components.perishable.onperishreplacement = "spoiled_food"

    MakeSmallBurnable(inst)
    MakeSmallPropagator(inst)

    MakeHauntableLaunchAndIgnite(inst)

    return inst
end

local weed_def = WEED_DEFS["daogui_ganoderma"]
-- 种子预制体函数
local function fn_seeds()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("pastoral_daogui_ganoderma")
    inst.AnimState:SetBuild("pastoral_daogui_ganoderma")
    inst.AnimState:PlayAnimation("seeds")
    inst.AnimState:SetRayTestOnBB(true)

    inst.pickupsound = "vegetation_firm"

    -- 可烹饪（来自可烹饪组件）已添加到原始状态以进行优化
    inst:AddTag("cookable")
    inst:AddTag("deployedplant")
    inst:AddTag("deployedfarmplant")
    inst:AddTag("oceanfishing_lure")

    inst.overridedeployplacername = "seeds_placer"

    inst.weed_def = weed_def

    inst._custom_candeploy_fn = can_plant_seed

    MakeInventoryFloatable(inst)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("edible")
    inst.components.edible.foodtype = FOODTYPE.SEEDS
    inst.components.edible.healthvalue = 2
    inst.components.edible.hungervalue = 2
    inst.components.edible.sanityvalue = 2

    inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

    inst:AddComponent("tradable")
    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")

    inst:AddComponent("perishable")
    inst.components.perishable:SetPerishTime(TUNING.PERISH_SUPERSLOW)
    inst.components.perishable:StartPerishing()
    inst.components.perishable.onperishreplacement = "spoiled_food"

    inst:AddComponent("cookable")
    inst.components.cookable.product = "seeds_cooked"

    inst:AddComponent("bait")

    inst:AddComponent("farmplantable")
    inst.components.farmplantable.plant = "weed_daogui_ganoderma"

    -- 已经弃用 (用于给旧档已经建造了的老版农场)
    inst:AddComponent("plantable")
    inst.components.plantable.growtime = TUNING.SEEDS_GROW_TIME
    inst.components.plantable.product = "daogui_ganoderma"

    -- 已经弃用 (现在是植物人专属)
    inst:AddComponent("deployable")
    inst.components.deployable:SetDeployMode(DEPLOYMODE.CUSTOM) -- 用函数 inst._custom_candeploy_fn
    inst.components.deployable.restrictedtag = "plantkin"
    inst.components.deployable.ondeploy = OnDeploy

    inst:AddComponent("oceanfishingtackle")
    inst.components.oceanfishingtackle:SetupLure({ build = "oceanfishing_lure_mis", symbol = "hook_seeds", single_use = true, lure_data = TUNING.OCEANFISHING_LURE.SEED })

    MakeSmallBurnable(inst)
    MakeSmallPropagator(inst)

    MakeHauntableLaunchAndPerish(inst)

    return inst
end

return Prefab("daogui_ganoderma", fn, assets),
    Prefab("daogui_ganoderma_seeds", fn_seeds, assets)
